package com.sangeng.controller;

import com.sangeng.entity.Menu;
import com.sangeng.entity.ResponseResult;
import com.sangeng.entity.User;
import com.sangeng.enums.AppHttpCodeEnum;
import com.sangeng.mapper.Exception.SystemException;
import com.sangeng.service.AdminLoginService;
import com.sangeng.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController

public class AdminLoginController {
    @Autowired
    AdminLoginService adminLoginService;
    @Autowired
    MenuService menuService;
    @PostMapping("/user/login")
    public ResponseResult login(@RequestBody User user){
      if(!StringUtils.hasText(user.getUserName()))
      {
          throw new SystemException(AppHttpCodeEnum.REQUIRE_USERNAME);

      }
      return   adminLoginService.login(user);

    }




    @GetMapping("/getInfo")
    public ResponseResult getInfo(){

        return adminLoginService.getInfo();
    }

    @GetMapping("/getRouters")

    public ResponseResult getRouters(){


        List<Menu> routers = menuService.getRouters();

        HashMap<String, List<Menu>> menusMap = new HashMap<>();
        menusMap.put("menus",routers);
        return ResponseResult.okResult(menusMap);

           }


    @RequestMapping("/user/logout")
    public ResponseResult logOut(){

       return adminLoginService.logOut();
    }

//    @PostMapping("/user/register")
//    public ResponseResult registerUser(@RequestBody User user){
//
//        return userService.registerUser(user);
//
//
//    }

}
